//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.momoc.netty.frame.handler;

import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.MessageLite;
import com.momoc.netty.frame.FrameMethodUtils;
import com.momoc.netty.frame.config.SmartNettyProperties;
import com.momoc.netty.frame.context.ProtobufHandlerMappingContext;
import com.momoc.netty.frame.dispatch.model.PacketMassageInfo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.util.internal.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * proto解码类
 */
@Sharable
public class ProtobufDecoder {
    static Logger log = LoggerFactory.getLogger(ProtobufDecoder.class);

    private static final boolean HAS_PARSER;
    private final MessageLite prototype;
    private final ExtensionRegistryLite extensionRegistry;

    public ProtobufDecoder(MessageLite prototype) {
        this(prototype, (ExtensionRegistry) null);
    }

    public ProtobufDecoder(MessageLite prototype, ExtensionRegistry extensionRegistry) {
        this(prototype, (ExtensionRegistryLite) extensionRegistry);
    }

    public ProtobufDecoder(MessageLite prototype, ExtensionRegistryLite extensionRegistry) {
        this.prototype = ((MessageLite) ObjectUtil.checkNotNull(prototype, "prototype")).getDefaultInstanceForType();
        this.extensionRegistry = extensionRegistry;
    }

    public MessageLite decode(ByteBuf msg)  {
        try {
            int length = msg.readableBytes();
            byte[] array;
            int offset;
            if (msg.hasArray()) {
                array = msg.array();
                offset = msg.arrayOffset() + msg.readerIndex();
            } else {
                array = ByteBufUtil.getBytes(msg, msg.readerIndex(), length, false);
                offset = 0;
            }

            if (this.extensionRegistry == null) {
                if (HAS_PARSER) {
                    return this.prototype.getParserForType().parseFrom(array, offset, length);
//                out.add(this.prototype.getParserForType().parseFrom(array, offset, length));
                } else {
                    return this.prototype.newBuilderForType().mergeFrom(array, offset, length).build();
                }
            } else if (HAS_PARSER) {
                return this.prototype.getParserForType().parseFrom(array, offset, length, this.extensionRegistry);
            } else {
                return this.prototype.newBuilderForType().mergeFrom(array, offset, length, this.extensionRegistry).build();
            }
        }catch (Exception e){
            log.error("send message to channel error", e);
            throw new RuntimeException(e);
        }

    }

    static {
        boolean hasParser = false;

        try {
            MessageLite.class.getDeclaredMethod("getParserForType");
            hasParser = true;
        } catch (Throwable var2) {
        }

        HAS_PARSER = hasParser;
    }

    public static PacketMassageInfo getRequestMessageInfo(ByteBuf byteBuf, Integer msgIdLength) {
        // 一个字节八位，所以要除以8
        int readMsgLength = msgIdLength / 8;
        Integer msgId = FrameMethodUtils.getMessageId(byteBuf, readMsgLength);
        //读消息号，目前消息号大小是16位，最多能定义65535个消息。
        PacketMassageInfo packetMassageInfo = ProtobufHandlerMappingContext.getRequestPacketInfoByMessageId(msgId);
        if (packetMassageInfo == null) {
            log.error(" This message does not have forwarder support, msgId:{}", msgId);
            return null;
        }
        return packetMassageInfo;
    }


    public static PacketMassageInfo getRequestMessageInfo(ByteBuf buf) {
        return getRequestMessageInfo(buf, SmartNettyProperties.getNettyProperties().getMsgIdLength());
    }







}
